package org.yelongframework.excel.data.fill.sheet.mode.forcopy.down;

import org.yelongframework.excel.data.fill.sheet.SheetDataFillException;
import org.yelongframework.excel.sheet.ExcelSheet;
import org.yelongframework.excel.workbook.ExcelWorkbook;

/**
 * 默认实现
 * 
 * @since 1.0.0
 */
public class DefaultDownForCopySheetDataFiller extends AbstractDownForCopySheetDataFiller {

	// ==================================================执行填充==================================================

	@Override
	protected void doDataFill(ExcelSheet excelSheet, DownForCopySheetDataFillScheme sheetDataFillScheme)
			throws SheetDataFillException {
		int beginRowIndex = sheetDataFillScheme.getBeginRowIndex();
		int beginColumnIndex = sheetDataFillScheme.getBeginColumnIndex();
		int endColumnIndex = sheetDataFillScheme.getEndColumnIndex();
		int forCount = sheetDataFillScheme.getForCount();
		Integer pageMaxForCount = sheetDataFillScheme.getPageMaxForCount();
		DownForCopySheetDataFillDataSupplier dataSupplier = sheetDataFillScheme.getDataSupplier();
		ExcelWorkbook excelWorkbook = excelSheet.getWorkbook();
		// 单Sheet填充
		if (null == pageMaxForCount || pageMaxForCount >= forCount) {
			excelSheet.copyRows(beginRowIndex + 1, (beginRowIndex + forCount) - 1, beginRowIndex + 2);
			for (int i = beginRowIndex; i < beginRowIndex + forCount; i++) {
				for (int j = beginColumnIndex; j <= endColumnIndex; j++) {
					Object value = dataSupplier.get0(i, j, i - beginRowIndex);
					excelSheet.setValue(i, j, value);
				}
			}
		} else { // 单Sheet分页填充
			// 总页数
			double pageTotalDouble = Math
					.ceil(Integer.valueOf(forCount).doubleValue() / Integer.valueOf(pageMaxForCount).doubleValue());
			int pageTotal = Double.valueOf(pageTotalDouble).intValue();
			// 页数为1只复制行
			if (pageTotal == 1) {
				excelSheet.copyRows(beginRowIndex + 1, (beginRowIndex + pageMaxForCount) - 1, beginRowIndex + 2);
			} else {// 页数大于1复制行和Sheet
				for (int i = 1; i < pageTotal; i++) {
					int copySheetIndex = excelSheet.getSheetIndex() + i;
					excelWorkbook.copySheet(excelSheet.getSheetIndex(), copySheetIndex);
					ExcelSheet copySheet = excelSheet.getWorkbook().getSheet(copySheetIndex);
					// 最后一页复制剩下的数量的行数
					if (i == pageTotal - 1) {
						copySheet.copyRows(beginRowIndex + 1,
								beginRowIndex + (forCount - pageMaxForCount * (pageTotal - 1)) - 1, beginRowIndex + 2);
					} else {// 非最后一页复制每页的数量的行数
						copySheet.copyRows(beginRowIndex + 1, (beginRowIndex + pageMaxForCount) - 1, beginRowIndex + 2);
					}
				}
				// 最后复制第一页的行。否则会把复制后的行赋值给其他Sheet
				excelSheet.copyRows(beginRowIndex + 1, (beginRowIndex + pageMaxForCount) - 1, beginRowIndex + 2);
			}
			// 开始填充数据
			for (int i = beginRowIndex; i < beginRowIndex + forCount; i++) {
				// 当前页码
				int currentPage = Double.valueOf(Math.ceil(Integer.valueOf(i - beginRowIndex + 1).doubleValue()
						/ Integer.valueOf(pageMaxForCount).doubleValue())).intValue();
				ExcelSheet currentExcelSheet;
				// 第一页正常填充
				if (currentPage == 1) {
					currentExcelSheet = excelSheet;
					for (int j = beginColumnIndex; j <= endColumnIndex; j++) {
						Object value = dataSupplier.get0(i, j, i - beginRowIndex);
						currentExcelSheet.setValue(i, j, value);
					}
				} else {// 之后页获取页码对应的Sheet进行填充
					currentExcelSheet = excelSheet.getWorkbook().getSheet(excelSheet.getSheetIndex() + currentPage - 1);
					for (int j = beginColumnIndex; j <= endColumnIndex; j++) {
						int currentRowIndex = i - pageMaxForCount * (currentPage - 1);
						Object value = dataSupplier.get0(currentRowIndex, j, i - beginRowIndex);
						currentExcelSheet.setValue(currentRowIndex, j, value);
					}
				}
			}
		}
	}

}
